package com.itheima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.util.RequestContextUtil;
import com.itheima.common.vo.PageResultVo;
import com.itheima.wemedia.dto.WmMaterialPageRequestDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.mapper.WmMaterialMapper;
import com.itheima.wemedia.service.WmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @description <p>自媒体图文素材信息 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.service.impl
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    /**
     * fastdfs图片存放的地址
     */
    @Value("${fastdfs-server}")
    private String fastdfsServer;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo findPage(WmMaterialPageRequestDto dto) {
        //1. 构建分页参数
        Page<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
        //2. 构建条件
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();
        // 只能查询登录用户自己的, 数据类要与数据库的一致，否则可能导致索引失效
        lqw.eq(WmMaterial::getUserId,RequestContextUtil.getUserId());
        // 只查收藏的
        lqw.eq(1==dto.getIsCollection(),WmMaterial::getIsCollection,dto.getIsCollection());
        //3. 执行查询
        page(page,lqw);
        //4. 给每个素材的图片，补全完整路径
        List<WmMaterial> list = page.getRecords();
        if (!CollectionUtils.isEmpty(list)){
            list.forEach(m ->m.setUrl(fastdfsServer+m.getUrl()));
        }
        //5. 构建返回的结果
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),page.getTotal(),list);
    }
}
